import { WS_URL } from "../configs";

function useWebSocket(handleWsMessage) {
  const ws = new WebSocket(WS_URL);

  const init = () => {
    bindEvent();
  }

  const bindEvent = () => {
    ws.addEventListener('open', handleWsOpen, false);
    ws.addEventListener('close', handleWsClose, false);
    ws.addEventListener('error', handleWsError, false);
    ws.addEventListener('message', handleWsMessage, false);
  }

  function handleWsOpen(e){
    console.log('webscoket open', e);
  }

  function handleWsClose(e) {
    console.log('websocket close', e);
  }

  function handleWsError(e) {
    console.log('websocket error', e);
  }

  init();
  return ws;
}

export {
  useWebSocket
}